package com.haier.dreambox.demo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.haier.dreambox.demo.R;
import com.haier.dreambox.lib.usdklib.udev.ac.ACCommand;
import com.haier.dreambox.lib.usdklib.udev.ac.ACOperation;
import com.haier.dreambox.lib.usdklib.wrapper.uPlusManager;
import com.haier.uhome.usdk.api.uSDKDeviceAttribute;
import org.androidannotations.annotations.*;
import org.androidannotations.annotations.res.ColorRes;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by smalant on 14/12/26.
 */

@EFragment(R.layout.layout_control_home_ac)
public class ControlHomeAcFragment extends Fragment implements IControlPageInterface {

    private static final String TAG = "ControlHomeAcFragment";
    @ViewById(R.id.id_tb_power)
    ToggleButton toggleButton;

    @ViewById(R.id.id_textview_dev_name)
    TextView textView;


    @ViewById(R.id.id_textView_temperature)
    TextView tv_temperature;

    @Bean
    ACOperation ac;

    @Bean
    ACCommand acCommand;


    @ViewById(R.id.id_seekbar_temperature)
    SeekBar seekBar;

    @ColorRes(android.R.color.white)
    int whiteColor;

    @ColorRes(android.R.color.holo_red_dark)
    int redColor;

    @Bean
    uPlusManager sdkManger;

    @ViewById(R.id.id_mode_air)
    Button button_air;

    @ViewById(R.id.id_mode_cool)
    Button button_cool;

    @ViewById(R.id.id_mode_heat)
    Button button_heat;

    @ViewById(R.id.id_mode_auto)
    Button button_auto;

    @ViewById(R.id.id_mode_dehumi)
    Button button_dehumi;


    @ViewById(R.id.id_wind_high)
    Button button_wind_high;

    @ViewById(R.id.id_wind_mid)
    Button button_wind_mid;

    @ViewById(R.id.id_wind_low)
    Button button_wind_low;

    @ViewById(R.id.id_wind_auto)
    Button button_wind_auto;

    @SeekBarProgressChange(R.id.id_seekbar_temperature)
    void onProgressChangeOnSeekBar(SeekBar seekBar, int progress, boolean fromUser) {

        Log.i(TAG, "progress: " + progress);

        if (fromUser) {
            ArrayList cmdList = new ArrayList();
            cmdList.add(new uSDKDeviceAttribute(ACCommand.CmdName.SETTING_TEMPERATURE, (16 + progress) + ""));
            ac.sendGroupCmd(mac, acCommand.getGroupAttributes(mac, cmdList));
        }
    }

    String mac;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @AfterViews
    public void initViews() {
        textView.setText("家用空调");

        if (mac != null && sdkManger.getDeviceAttributeValues(mac) != null) {
            refreshControlView(sdkManger.getDeviceAttributeValues(mac));
        }
    }

    @AfterInject
    public void init() {
        mac = getArguments().getString("mac");
    }


    @CheckedChange(R.id.id_tb_power)
    void checkedChangeOnPowerToggleButton(CompoundButton power, boolean isChecked) {
        if (isChecked) {
            ac.closeDevice(mac);
        } else {
            ac.openDevice(mac);
        }
    }


    @Click({R.id.id_wind_auto, R.id.id_wind_high, R.id.id_wind_mid, R.id.id_wind_low})
    void setWind(View clickedView) {

        switch (clickedView.getId()) {
            case R.id.id_wind_auto:
                ac.setAcWind(mac, ACCommand.AcWind.AUTO);
                break;
            case R.id.id_wind_low:
                ac.setAcWind(mac, ACCommand.AcWind.LOW);
                break;
            case R.id.id_wind_high:
                ac.setAcWind(mac, ACCommand.AcWind.HIGH);
                break;
            case R.id.id_wind_mid:
                ac.setAcWind(mac, ACCommand.AcWind.MID);
                break;
        }
    }

    @Click({R.id.id_mode_air, R.id.id_mode_auto, R.id.id_mode_cool, R.id.id_mode_heat, R.id.id_mode_dehumi})
    void setMode(View clickedView) {
        switch (clickedView.getId()) {
            case R.id.id_mode_auto:
                ac.setAcMode(mac, ACCommand.AcMode.AUTO);
                break;
            case R.id.id_mode_cool:
                ac.setAcMode(mac, ACCommand.AcMode.COOL);
                break;
            case R.id.id_mode_heat:
                ac.setAcMode(mac, ACCommand.AcMode.HEAT);
                break;
            case R.id.id_mode_dehumi:
                ac.setAcMode(mac, ACCommand.AcMode.DEHUMI);
                break;
            case R.id.id_mode_air:
                ac.setAcMode(mac, ACCommand.AcMode.AIR);
                break;
        }
    }

    @Override
    public void refreshControlView(HashMap<String, uSDKDeviceAttribute> attributes) {
        if (attributes.get(ACCommand.CmdName.SETTING_TEMPERATURE) != null) {
            //设置温度
            String temperature = attributes.get(ACCommand.CmdName.SETTING_TEMPERATURE).getAttrvalue();
            tv_temperature.setText(temperature);
            seekBar.setProgress(Integer.parseInt(temperature) - 16);
        }

        if (attributes.get(ACCommand.CmdName.SETTING_MODE) != null) {
            //模式
            String mode = attributes.get(ACCommand.CmdName.SETTING_MODE).getAttrvalue();
            if (ACCommand.CmdValue.SETTING_MODE_AIR.equals(mode)) {
                setModeButtonBgColor(button_air);
            } else if (ACCommand.CmdValue.SETTING_MODE_AUTO.equals(mode)) {
                setModeButtonBgColor(button_auto);
            } else if (ACCommand.CmdValue.SETTING_MODE_DEHUMIDIFICATION.equals(mode)) {
                setModeButtonBgColor(button_dehumi);
            } else if (ACCommand.CmdValue.SETTING_MODE_HOT.equals(mode)) {
                setModeButtonBgColor(button_heat);
            } else if (ACCommand.CmdValue.SETTING_MODE_COOL.equals(mode)) {
                setModeButtonBgColor(button_cool);
            }

        }

        if (attributes.get(ACCommand.CmdName.SETTING_WIND_SPEED) != null) {
            //模式
            String mode = attributes.get(ACCommand.CmdName.SETTING_WIND_SPEED).getAttrvalue();
            if (ACCommand.CmdValue.SETTING_WIND_SPEED_AUTO.equals(mode)) {
                setWindButtonBgColor(button_wind_auto);
            } else if (ACCommand.CmdValue.SETTING_WIND_SPEED_HIGH.equals(mode)) {
                setWindButtonBgColor(button_wind_high);
            } else if (ACCommand.CmdValue.SETTING_WIND_SPEED_MIDDLE.equals(mode)) {
                setWindButtonBgColor(button_wind_mid);
            } else if (ACCommand.CmdValue.SETTING_WIND_SPEED_LOW.equals(mode)) {
                setWindButtonBgColor(button_wind_low);
            }

        }

    }

    private void setModeButtonBgColor(TextView view) {
        button_auto.setTextColor(whiteColor);
        button_air.setTextColor(whiteColor);
        button_dehumi.setTextColor(whiteColor);
        button_heat.setTextColor(whiteColor);
        button_cool.setTextColor(whiteColor);
        view.setTextColor(redColor);
    }

    private void setWindButtonBgColor(TextView view) {
        button_wind_high.setTextColor(whiteColor);
        button_wind_low.setTextColor(whiteColor);
        button_wind_mid.setTextColor(whiteColor);
        button_wind_auto.setTextColor(whiteColor);
        view.setTextColor(redColor);
    }

    @Override
    public String getBindMac() {
        return mac;
    }


}
